function checkName() {
    axios.get("/user/findByName", {
        params: {
            name: $("name").value
        }
    }).then(resp => {
        var info = resp.data;
        if (info == "ok") {
            $("nameSpan").style.color = "green";
            $("nameSpan").innerHTML = "通过";
        } else {
            $("nameSpan").style.color = "red";
            $("nameSpan").innerHTML = "该用户名已被注册";
        }
    })
}

function check() {
    let rex1 = /^.{1,10}$/;
    let rex2 = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    if (rex1.test($("pwd").value)) {
        $("pwdSpan").style.color = "green";
        $("pwdSpan").innerHTML = "通过";
    } else {
        $("pwdSpan").style.color = "red";
        $("pwdSpan").innerHTML = "请输入1-10个字符";
    }

    if ($("pwd").value == $("sure").value) {
        $("sureSpan").style.color = "green";
        $("sureSpan").innerHTML = "通过";
    } else {
        $("sureSpan").style.color = "red";
        $("sureSpan").innerHTML = "密码不一致";
    }

    if (rex2.test($("email").value)) {
        $("emailSpan").style.color = "green";
        $("emailSpan").innerHTML = "通过";
    } else {
        $("emailSpan").style.color = "red";
        $("emailSpan").innerHTML = "邮箱格式不正确";
    }
}

async function register() {
    await this.checkName();
    await this.check();
    if ($("nameSpan").innerHTML=="通过" &&
        $("pwdSpan").innerHTML=="通过" &&
        $("sureSpan").innerHTML=="通过" &&
        $("emailSpan").innerHTML=="通过") {
        let head;
        let obj = document.getElementsByClassName("head");
        for (let i = 0; i < obj.length; i++) {
            if (obj[i].checked) {
                head = obj[i].value;
            }
        }
        axios.get("/user/add", {
            params: {
                name: $("name").value,
                pwd: $("pwd").value,
                email: $("email").value,
                head: head
            }
        }).then(resp => {
            var info = resp.data;
            if (info == "ok") {
                location.href = "/login.html";
            }
        });
    } else {
        $("errorSpan").style.color = "red";
        $("errorSpan").innerHTML = "注册失败";
    }
}



